* Figure A5: Sensitivity to Father's Age When Income Measured
clear
use "${data_dir}DavisMazumderData.dta"

keep if age_firstSurvey<=18
drop if miss0 | miss1
drop if (mom_outOfRange==1 & linkMom==1 & !(dad_outOfRange==0 & linkDad==1)) | (dad_outOfRange==1 & linkDad==1 & !(mom_outOfRange==0 & linkMom==1)) | (linkMom==0 & linkDad==0) 
 

 * Generate income ranks after applying sample restrictions
gen N = .
 tab n  if surv79==0 & women==0  [w=weight]
 replace N = r(N) if surv79==0 & women==0 
 tab n  if surv79==0 & women==1   [w=weight]
 replace N = r(N) if surv79==0 & women==1 
 tab n  if surv79==1 & women==0   [w=weight]
 replace N = r(N) if surv79==1 & women==0 
 tab n  if surv79==1 & women==1 [w=weight]
 replace N = r(N) if surv79==1 & women==1 
 
* Family Income Rank in Parent Generation
set sortseed 6688571
sort women surv79 faminc0
by women surv79: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
by women surv79 faminc0: egen rank0 = mean(_rank0) 
 replace rank0 = 100*rank0

gen perc79 = rank0*surv79

* Family Income Rank in Kid Generation
set sortseed 6688571
sort women surv79 faminc1
by women surv79: gen __rank1 = sum(weight)
gen _rank1 = __rank1/N 
bys women surv79 faminc1: egen rank1 = mean(_rank1) 
replace rank1 = 100*rank1

drop N __rank0 _rank0 __rank1 _rank1 


* -------------------- *
* Ranks by Birth Year  *
* -------------------- *

bys surv79 women byear: egen N=sum(weight)

set sortseed 6688571 
sort women surv79 byear faminc0
by women surv79 byear: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
bys women surv79 byear faminc0: egen rank0_byear = mean(_rank0) 

set sortseed 6688571 
sort women surv79 byear faminc1
by women surv79 byear: gen __rank1 = sum(weight) 
gen _rank1 = __rank1/N 
bys women surv79 byear faminc1: egen rank1_byear = mean(_rank1) 

gen perc66_byear = rank0_byear*(1-surv79)
gen perc79_byear = rank0_byear*surv79

drop N __rank0 _rank0 __rank1 _rank1


* ---------------- *
* Ranks by Dad Age *
* ---------------- *

bys surv79 women dad_age1_b: egen N=sum(weight)

set sortseed 6688571
sort women surv79 dad_age1_b faminc0
by women surv79 dad_age1_b: gen __rank0 = sum(weight) 
gen _rank0 = __rank0/N 
bys women surv79 dad_age1_b faminc0: egen rank0_dad_age1_b = mean(_rank0) 

set sortseed 6688571 
sort women surv79 dad_age1_b faminc1
by women surv79 dad_age1_b: gen __rank1 = sum(weight) 
gen _rank1 = __rank1/N 
bys women surv79 dad_age1_b faminc1: egen rank1_dad_age1_b = mean(_rank1) 

gen perc66_dad_age1_b = rank0_dad_age1_b*(1-surv79)
gen perc79_dad_age1_b = rank0_dad_age1_b*surv79


drop N __rank0 _rank0 __rank1 _rank1 


* --------- Dad age when income measured--------- *


matrix rank_diff_byear = J(10,3,.)
matrix ige_diff = J(10,3,.)


replace perc79_byear = perc79_byear*100

local r=0
forv a=40/49 {
 local r=`r'+1

 matrix rank_diff_byear[`r',1]=`a'
 reg rank1_dad_age1_b rank0_dad_age1_b perc79_dad_age1_b  surv79 women if dad_age1_b==`a'  [w=weight], cluster(hhid)
 matrix rank_diff_byear[`r',2] = _b[perc79_dad_age1_b]
 matrix rank_diff_byear[`r',3] = _se[perc79_dad_age1_b] 
 
 matrix ige_diff[`r',1]=`a'
 qui reg lfaminc1 lfaminc0 loginc79 surv79 women if dad_age1_b==`a'  [w=weight], cluster(hhid)
 matrix ige_diff[`r',2] = _b[loginc79]
 matrix ige_diff[`r',3] = _se[loginc79]
 
 
}

 



clear
svmat rank_diff_byear

ren rank_diff_byear1 age
ren rank_diff_byear2 diff
gen lb =  diff-1.96*rank_diff_byear3
gen ub =  diff+1.96*rank_diff_byear3


graph twoway   (rarea lb ub age , fcolor(gs15) lcolor(gs15) mcolor(gs15) ) (connected diff age, mcolor(black) lcolor(black) ) (function y=0.12, range(40 49) lcolor(black) lpattern(dash)), ///
	graphregion(color(white)) ylabel(-.3(0.15)0.6, nogrid) xtitle("Father age when income first measured") ytitle("Change in Rank-Rank Slope")  xlabel(40(1)49) ///
	legend(order( 2 "Change in Rank-Rank Slope" 1 "95 Percent CI"  3 "Main Estimate") pos(6) col(2)) 
	
graph export ${results_dir}figureA5_rank.png, replace





clear
svmat ige_diff

ren ige_diff1 age
ren ige_diff2 diff
gen lb =  diff-1.96*ige_diff3
gen ub =  diff+1.96*ige_diff3


graph twoway   (rarea lb ub age , fcolor(gs15) lcolor(gs15) mcolor(gs15) ) (connected diff age, mcolor(black) lcolor(black) ) (function y=0.29, range(40 49) lcolor(black) lpattern(dash)), ///
	graphregion(color(white)) ylabel(-.6(0.3)0.9, nogrid) xtitle("Father age when income first measured") ytitle("Change in IGE")  xlabel(40(1)49) ///
	legend(order( 2 "Change in IGE" 1 "95 Percent CI"  3 "Main Estimate") pos(6) col(2))
	
graph export ${results_dir}figureA5_ige.png, replace












































